import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { AxiosService } from '../common/axios/axios.service';
import { DecodeDto } from './dto';

@Injectable()
export class MapService {
  private readonly host: string;
  private readonly ak: string;
  private readonly browserAk: string;
  constructor(
    private readonly configService: ConfigService,
    private readonly axiosService: AxiosService,
  ) {
    this.host = this.configService.get<string>('map.baidu.host') as string;
    this.ak = this.configService.get<string>('map.baidu.ak') as string;
    this.browserAk = this.configService.get<string>('map.baidu.browserAk') as string;
  }

  // 经纬度转地址
  async decode(decodeDto: DecodeDto) {
    const { lon, lat, extensions_poi = '1' } = decodeDto;
    const location = `${lon},${lat}`;
    const url = `${this.host}/reverse_geocoding/v3?output=json`;
    return await this.axiosService.get(url, { ak: this.ak, location, extensions_poi });
  }

  // 地址转经纬度
  encode() {}

  // 百度地图jsApi代理
  // 1. 解决AK泄露问题
  async jsApiProxy() {
    const requestUrl = `${this.host}/getscript`;
    return await this.axiosService.get(requestUrl, {
      v: '1.0',
      type: 'webgl',
      ak: this.browserAk,
    });
  }
}
